package com.tuananh.radnom.persist.imp.xml;

import java.util.ArrayList;

import com.ajexperience.utils.DeepCopyException;
import com.ajexperience.utils.DeepCopyUtil;
import com.tuananh.radnom.model.NetConn;
import com.tuananh.radnom.model.NetElementConfiguration;
import com.tuananh.radnom.model.NetInterface;
import com.tuananh.radnom.model.NetNode;
import com.tuananh.radnom.model.event.EventReporter;
import com.tuananh.radnom.model.event.NeEvent;
import com.tuananh.radnom.persist.INetConnDAO;

public class NetConnXmlDAO extends XmlDAO implements INetConnDAO {
	NetElementConfiguration netConfig;

	public NetConnXmlDAO() {
		super(NetElementConfiguration.path);
		configuration = XmlConfigHolder.neConfig;
		try {
			savedConfiguration = (new DeepCopyUtil()).deepCopy(configuration);
		} catch (DeepCopyException e) {
			// TODO Auto-generated catch block
			savedConfiguration = new NetElementConfiguration();
			e.printStackTrace();
		}
		netConfig = (NetElementConfiguration) configuration;
	}
	public String getName() {
		return "NetConnEvent";
	}
	public boolean add(NetInterface parent, NetInterface child) {
		// TODO Auto-generated method stub
		NetConn newConn = new NetConn();
		newConn.setParent(parent);
		newConn.setChild(child);
		newConn.setName(parent.getName() + "--" + child.getName());
		ArrayList<NetConn> listConn = netConfig.getListConn();
		for (NetConn conn : listConn) {
			if (conn.getName().equals(newConn.getName()))
				return false;
		}
		netConfig.getListConn().add(newConn);
		reportEvent(newConn, generateHeaders("METHOD_NAME", "add"));
		return true;
	}

	public boolean add(NetConn netConn) {
		// TODO Auto-generated method stub
		add(netConn.getParent(), netConn.getChild());
		return true;
	}

	public boolean update(NetConn netConn) {
		// TODO Auto-generated method stub
		ArrayList<NetConn> listConn = netConfig.getListConn();
		for(int i=0;i<listConn.size();i++)
		{
			if (listConn.get(i).getName().equals(netConn.getName())){
				if ((new NetConn()).validate(netConn)){
					listConn.set(i, netConn);
					reportEvent(netConn, generateHeaders("METHOD_NAME", "update"));
					return true;
				}
			}
		}
		return false;
	}

	public boolean remove(String name) {
		// TODO Auto-generated method stub
		ArrayList<NetConn> listConn = netConfig.getListConn();
		for(NetConn conn:listConn){
			if (conn.getName().equals(name)) 
				{
					listConn.remove(conn);
					reportEvent(conn, generateHeaders("METHOD_NAME", "remove"));
					return true;
				}
		}
		return false;
	}

	public ArrayList<NetConn> getAll() {
		// TODO Auto-generated method stub
		return netConfig.getListConn();
	}

	public NetConn getByName(String name) {
		// TODO Auto-generated method stub
		ArrayList<NetConn> listConn = netConfig.getListConn();
		for(int i=0;i<listConn.size();i++)
		{
			if (listConn.get(i).getName().equals(name))
				return listConn.get(i);
		}
		return null;
	}

}
